<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BSResource
 *
 * @author jianfeng
 */
class BSResource extends BSEntityWithGroup {

    //put your code here
    public $name = 'no-name';
    public $amount = 0;
    public $description = 'no-description';
    public $creatorId = 0;
    public $createTime = null;
    public $spaceId = 0;
    public $appId = 0;

    public function initBeforeLoad($data = NULL) {
        if ($data != 'notInitBeforeLoad') {
            parent::initBeforeLoad($data);
            self::initInput();
        }
    }

    public static function initInput($data = NULL) {

        $spaceId = Blyn::app()->getActiveSpace()->getId();
        $appId = Blyn::app()->getActiveApp()->getId();
        $creatorId = Blyn::app()->getCurrentUser()->getId();

        $r = new BSResource('notInitBeforeLoad');
        $r->name = '1号足球场地';
        $r->spaceId = $spaceId;
        $r->appId = $appId;
        $r->creatorId = $creatorId;
        $r->save();
        BPermit::addPermit($r, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

        $resourceGroup = BSResourceGroup::find('Sport Type.十一人足球');
        $resourceGroup->addMember($r);

        $resourceGroup = BSResourceGroup::find('Area.重庆.九龙坡区');
        $resourceGroup->addMember($r);

        $resourceGroup = BSResourceGroup::find('Sport Stadium.奥体体育公园运动中心');
        $resourceGroup->addMember($r);

        $r = new BSResource();
        $r->name = '2号足球场地';
        $r->spaceId = $spaceId;
        $r->appId = $appId;
        $r->creatorId = $creatorId;
        $r->save();
        BPermit::addPermit($r, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

        $resourceGroup = BSResourceGroup::find('Sport Type.十一人足球');
        $resourceGroup->addMember($r);

        $resourceGroup = BSResourceGroup::find('Area.重庆.九龙坡区');
        $resourceGroup->addMember($r);

        $resourceGroup = BSResourceGroup::find('Sport Stadium.奥体体育公园运动中心');
        $resourceGroup->addMember($r);

        $r = new BSResource();
        $r->name = '羽毛球场地';
        $r->spaceId = $spaceId;
        $r->appId = $appId;
        $r->creatorId = $creatorId;
        $r->save();
        BPermit::addPermit($r, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

        $resourceGroup = BSResourceGroup::find('Sport Type.羽毛球');
        $resourceGroup->addMember($r);

        $resourceGroup = BSResourceGroup::find('Area.重庆.渝中区');
        $resourceGroup->addMember($r);

        $resourceGroup = BSResourceGroup::find('Sport Stadium.重庆市体育馆');
        $resourceGroup->addMember($r);
    }

    public function save($context = null) {

        if ($this->spaceId <= 0)
            $this->spaceId = Blyn::app()->getActiveSpace()->getId();
        if ($this->appId <= 0)
            $this->appId = $appId = Blyn::app()->getActiveApp()->getId();
        if ($this->creatorId <= 0)
            $this->creatorId = $creatorId = Blyn::app()->getCurrentUser()->getId();
        if ($this->createTime == NULL)
            $this->createTime = time();
        if ($this->amount == 0)
            $this->amount = 1;

        parent::save($context);
    }

    public function getGroups($context = NULL, $recursive = FALSE) {

        if (!$context instanceof BGroup) {
            $groupClass = $this->getGroupClass();
            if (is_array($context)) {
                if (isset($context['groupClass']) && $context['groupClass'] != NULL)
                    $groupClass = $context['groupClass'];
                if (!isset($context['classname']))
                    $context['classname'] = $groupClass;
            }
            $context = $groupClass::getEntityContext($context);
        }

        $groups = BRepositoryFactory::createRepository2()->findMemberGroups($this, $context);

        if ($groups != NULL && $recursive) {
            $recursiveItems = $groups;
            foreach ($groups as $group) {
                if ($group instanceof BTreeEntity) {
                    $parentGroups = $group->getParent($recursive);
                    foreach ($parentGroups as $item) {
                        if ($item->getId() !== 1)//ignore root node
                            $recursiveItems[$item->getId()] = $item;
                    }
                }
            }
            if ($recursiveItems != NULL) {
                return $recursiveItems;
            }
        }

        if ($groups != NULL) {
            foreach ($groups as $key => $item)
                if ($item->getId() === 1)//ignore root node
                    unset($groups[$key]);
            return $groups;
        }
    }
    //get default group classname
    public function getGroupClass($data = NULL) {
        $class = get_class($this);
        if (preg_match("/Member$/", $class))
            return preg_replace("/Member/", "", $class);
        else
            return $class . 'Group';
    }
    
    public function getModelClassAsGroupMember($group) {
        return $group->getMemberModelClass();
    }
}
